package com.example.opencvdemo


import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import org.opencv.android.Utils
import org.opencv.core.Mat
import org.opencv.core.Rect
import org.opencv.imgproc.Imgproc


@Composable
fun CutCompose(modifier: Modifier = Modifier) {
    val context = LocalContext.current
    Column(
        modifier = modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        var src = Mat()
        try {
            src = Utils.loadResource(context, R.drawable.pic01)
        } catch (e: Exception) {
            e.printStackTrace()
            return
        }
        // 手动指定切割区域
        val rect = Rect(55, 108, 365, 269)
        val dst = Mat(src, rect)
        //转换回RGB颜色
        Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2RGB)
        val resultBitmap = Bitmap.createBitmap(src.width(), src.height(), Bitmap.Config.ARGB_8888)
        Utils.matToBitmap(src, resultBitmap)
        Image(
            bitmap = resultBitmap.asImageBitmap(), contentDescription = null
        )
        val resultBitmap1 = Bitmap.createBitmap(dst.width(), dst.height(), Bitmap.Config.ARGB_8888)
        Utils.matToBitmap(dst, resultBitmap1)
        Image(
            bitmap = resultBitmap1.asImageBitmap(), contentDescription = null
        )
    }
}
